In time, you will call me master -- Star Wars
Week 1 Invert Binary Tree
Solution 1
class Solution:
def invertTree(self, root: TreeNode) -> TreeNode:
invert_node = TreeNode()
queue = []
queue.append(root)
while(queue):
tmp_queue = queue.pop(0)
if tmp_queue:
tmp_queue.left, tmp_queue.right = tmp_queue.right, tmp_queue.left
queue.append(tmp_queue.left)
queue.append(tmp_queue.right)
return root
Solution 2
from collections import deque
class Solution:
def invertTree(self, root: TreeNode) -> TreeNode:
invert_node = TreeNode()
queue = deque([root])
while queue:
item = queue.popleft()
if item:
item.left, item.right = item.right, item.left
queue.append(item.left)
queue.append(item.right)
return root
important take away